package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ChapterItemBankDO;
import com.jinzhi.jzweb.domain.TrainingPapersDO;
import com.jinzhi.jzweb.domain.TrainingQuestionsDO;
import com.jinzhi.jzweb.service.ChapterItemBankService;
import com.jinzhi.jzweb.service.TrainingPapersService;
import com.jinzhi.jzweb.service.TrainingQuestionsService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 
 * <pre>
 * 培训试题表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trainingQuestions")
public class TrainingQuestionsController extends BaseController {
	@Autowired
	private TrainingQuestionsService trainingQuestionsService;
	@Autowired
	private TrainingPapersService trainingPapersService;
	@Autowired
	private ChapterItemBankService chapterItemBankService;

	@GetMapping()
	@RequiresPermissions("jzweb:trainingQuestions:trainingQuestions")
	String TrainingQuestions(String id,String type,Model model){
		model.addAttribute("tpaperId",id);
		model.addAttribute("type",type);
		return "jzweb/trainingQuestions/trainingQuestions";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:trainingQuestions:trainingQuestions")
	public Result<Page<TrainingQuestionsDO>> list(TrainingQuestionsDO trainingQuestionsDTO){
		// 查询列表数据
		Page<TrainingQuestionsDO> page = trainingQuestionsService.selectPage(getPage(TrainingQuestionsDO.class),
				trainingQuestionsService.convertToEntityWrapper("tpapers_id", trainingQuestionsDTO.getTpapersId(),"introduce", trainingQuestionsDTO.getIntroduce()
				).orderBy("id", true));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			TrainingQuestionsDO obj = (TrainingQuestionsDO) page.getRecords().get(i);
			if (obj.getTpapersId() != null){
				TrainingPapersDO trainingPapersDO = trainingPapersService.selectById(obj.getTpapersId());
				if (trainingPapersDO != null) {
					obj.setTpapersName(trainingPapersDO.getName());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:trainingQuestions:add")
	String add(Long pid,Model model){
		TrainingPapersDO trainingPapersDO = trainingPapersService.selectById(pid);
		if (trainingPapersDO != null) {
			model.addAttribute("tpaperName",trainingPapersDO.getName());
		}
		model.addAttribute("tpapersId",pid);
	    return "jzweb/trainingQuestions/add";
	}

	@Log("查询章节题目信息")
	@ResponseBody
	@GetMapping("/selChapItemBanks")
	public Result<?> selChapItemBanks(String pageNumber, String pageSize, String tpaperId,String introduce) {
		TrainingPapersDO trainingPapersDO = trainingPapersService.selectById(Long.parseLong(tpaperId));
		if (trainingPapersDO != null) {
			List<Long> cibathId = trainingQuestionsService.selCibankIdList(trainingPapersDO.getId());
			Map map = new HashMap();
			map.put("coursesId",trainingPapersDO.getCoursesId());
			if(cibathId != null && cibathId.size()>0){
				map.put("cibankId",cibathId);
			}else{
				map.put("cibankId",null);
			}
			if(StringUtils.isNotBlank(introduce)){
				map.put("introduce",introduce);
			}else{
				map.put("introduce",null);
			}

			map.put("type",trainingPapersDO.getType());

			Page<Map> page = chapterItemBankService.selCibankIdLists(Integer.parseInt(pageNumber), Integer.parseInt(pageSize),map);
			return Result.ok(page);
		}

		return Result.fail("请先选择认证批次！");
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:trainingQuestions:edit")
	String edit(@PathVariable("id") Long id,Model model){
		TrainingQuestionsDO trainingQuestions = trainingQuestionsService.selectById(id);
		model.addAttribute("trainingQuestions", trainingQuestions);
		TrainingPapersDO trainingPapersDO = trainingPapersService.selectById(trainingQuestions.getTpapersId());
		if (trainingPapersDO != null) {
			model.addAttribute("tpaperName",trainingPapersDO.getName());
		}
	    return "jzweb/trainingQuestions/edit";
	}
	
	@Log("添加培训试题表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:trainingQuestions:add")
	public Result<String> save(@RequestParam("ids[]") Long[] ids, @RequestParam("tpaperId") String tpaperId){
		if(ids != null && ids.length > 0){
			List<TrainingQuestionsDO> traList = new ArrayList<>();
			for(Long id : ids){
				ChapterItemBankDO chapterItemBankDO = chapterItemBankService.selectById(id);
				TrainingQuestionsDO trainingQuestionsDO = new TrainingQuestionsDO();
				trainingQuestionsDO.setType(chapterItemBankDO.getTopicTypes());//题目类型（0单选，1多选，2判断）
				trainingQuestionsDO.setState(0);//默认正常
				trainingQuestionsDO.setIntroduce(chapterItemBankDO.getContents());//题目内容
				trainingQuestionsDO.setOptions(chapterItemBankDO.getOptions());//答案选项
				trainingQuestionsDO.setRightKey(chapterItemBankDO.getRightKey());//正确答案
				trainingQuestionsDO.setTopicAnalysis(chapterItemBankDO.getTopicAnalysis());//题目解析
				trainingQuestionsDO.setScore(chapterItemBankDO.getScore());//分值
				trainingQuestionsDO.setCibankId(chapterItemBankDO.getId());//题目id
				trainingQuestionsDO.setTpapersId(Long.parseLong(tpaperId));//试卷id
				List<TrainingQuestionsDO> trainingQuestionsDOS = trainingQuestionsService.findByKv("tpapers_id",Long.parseLong(tpaperId));
				trainingQuestionsDO.setTitleNumber(trainingQuestionsDOS.size() + 1);
				traList.add(trainingQuestionsDO);
			}
			if(traList != null && traList.size() > 0){
				boolean b = trainingQuestionsService.insertBatch(traList,traList.size());
				if(b){
					return Result.ok();
				}else{
					return Result.fail("保存错误");
				}
			}
		}

        return Result.ok();
	}
	
	@Log("修改培训试题表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:trainingQuestions:edit")
	public Result<String>  update( TrainingQuestionsDO trainingQuestions){
		boolean update = trainingQuestionsService.updateById(trainingQuestions);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除培训试题表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainingQuestions:remove")
	public Result<String>  remove( Long id){
		trainingQuestionsService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除培训试题表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainingQuestions:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		trainingQuestionsService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
